home *** CD-ROM | disk | FTP | other *** search
- Path: kuhub.cc.ukans.edu!anh
- From: anh@kuhub.cc.ukans.edu
- Newsgroups: comp.lang.c
- Subject: Re: Tradition or what?
- Message-ID: <1996Mar8.153250.115645@kuhub.cc.ukans.edu>
- Date: 8 Mar 96 15:32:49 CST
- References: <4g0elg$mdr@redstone.interpath.net> <4hpd8a$d70@alterdial.UU.NET>
- Organization: University of Kansas Academic Computing Services
-
- Well, I found one good use of magic numbers such as when one needs a
- localized temporary buffer of data.
-
- int func()
- {
- FILE *fp;
- char buf[15+1];
-
- ...
-
- fgets(buf,15,fp);
- }
-
-
- Well, if I know the data is always going to be less than 15 or whatever,
- there is really no need to use a #define here.
-
- CAnh
-
- In article <4hpd8a$d70@alterdial.UU.NET>, rogerst@approach.com (Tom Rogers) writes:
- > softbase@mercury.interpath.net (Scott McMahan - Softbase Systems)
- > wrote:
- >
- >>Larry Weiss (lfw@oc.com) wrote:
- >>
- >>: > Actually, the second rule is quite good (except that '2' should be replaced
- >>: > by '1' :-) Magic constants embedded in the code are a huge nuisance
- >>: > for maintenability. [...]
- >
- >>: I agree that there are good uses for giving a literal a name, and using
- >>: that name instead of repeated usage of the literal, but to make a
- >>: mandate that even if the literal were referenced only once you must give
- >>: it a name would seem too extreme.
- >
- >>From personal experience, I think you can't be too extreme. I've been
- >>burned by literals before. I've come to the conclusion that magic
- >>numbers in the code are evil and should be done away with. Anything
- >>that isn't a universal, unchanging constant (like an integer being 32
- >>bits :)), something like a law of physics, needs to be a constant in
- >>the program. I always #define it, too, since C has broken const
- >>constants that can't be used as initializers (which defeats the whole
- >>point of const).
- >
- >>Scott
- >
- > I agree, Scott. Magic numbers are evil. Don't use them not even
- > once. Your code will be reused, by you or someone else. Even if
- > you just use a magic number in one place it doesn't mean that in
- > the future your routines won't get included in some other module
- > with other new routines that need to use the 'same' magic number.
- >
- > Furthermore, what does 5 or 7 or 10 mean? It is NOT self documenting.
- > Make it a well named constant and reduce the need for accompanying
- > comments describing what the value is.
- >
- > Tom
- >
- >
-